/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.privacysandbox.tools.testing

import androidx.room.compiler.processing.util.Source
import java.io.File

/** Load files in the given directory as Room's [Source]s. */
fun loadSourcesFromDirectory(directory: File): List<Source> {
    check(directory.exists()) { "${directory.path} doesn't exist." }
    check(directory.isDirectory) { "${directory.path} is not a directory." }
    return directory.walk().filter { it.isFile }.map {
        val relativePath = directory.toPath().relativize(it.toPath()).toString()
        val qualifiedName = relativePath.removeSuffix(".${it.extension}").replace('/', '.')
        Source.load(file = it, qName = qualifiedName, relativePath = relativePath)
    }.toList()
}

/** Load files in the given directory as pairs <relative path, content>. */
fun loadFilesFromDirectory(directory: File): List<Pair<String, String>> {
    check(directory.exists()) { "${directory.path} doesn't exist." }
    check(directory.isDirectory) { "${directory.path} is not a directory." }
    return directory.walk().filter { it.isFile }.map {
        directory.toPath().relativize(it.toPath()).toString() to it.readText()
    }.toList()
}
